package com.javaspeak.concurrency.lock.custom.reentrant;

import java.util.concurrent.Callable;

/**
 * The IncrementCounterCallable runs in its own thread and is started by the
 * ExecutorService which manages the initializing and starting of threads.
 * <p>
 * This callable makes a call to the Counter's
 * counter.getDoubleIncrement( timeoutMilliseconds ) method and then returns
 * the CounterValue object.
 * <p>
 * The ExecutorService which invoked the callable will be able to retrieve the
 * CounterValue from a Future.
 *
 * @author John Dickerson
 */
public class IncrementCounterCallable implements Callable<CounterValue> {

    private Counter counter;
    private int timeoutMilliseconds;

    public IncrementCounterCallable( Counter counter, int timeoutMilliseconds ){

        this.counter = counter;
        this.timeoutMilliseconds = timeoutMilliseconds;
    }


    @Override
    public CounterValue call() throws Exception {

        int value = counter.getDoubleIncrement( timeoutMilliseconds );
        long nanoTime = System.nanoTime();

        return new CounterValue( value, nanoTime );
    }
}
